package com.caojing.haolemai.ui.outlets.outletsdetail;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.caojing.haolemai.R;
import com.caojing.haolemai.bean.outletsdetailsbean.OutletsDeatailsBean;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by CaoJing on 2016/10/18.
 */
public class OutletsDetailGvAdapter extends BaseAdapter {
    private List<OutletsDeatailsBean.ResultsEntity.ResultsEntity1> list;
    private Context context;

    public OutletsDetailGvAdapter(List<OutletsDeatailsBean.ResultsEntity.ResultsEntity1> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int i) {
        return list != null ? list.get(i) : null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.home_detail_gv_item, null);//todo:这里是复用了首页详情的布局
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

//        加载数据
        OutletsDeatailsBean.ResultsEntity.ResultsEntity1 resultsEntity1 = list.get(position);
        String name = resultsEntity1.getName();
        String listImage = resultsEntity1.getListImage();
        String salePrice = resultsEntity1.getSalePrice();
        String marketPrice = resultsEntity1.getMarketPrice();
        String discount = resultsEntity1.getDiscount();

        Glide.with(context).load(listImage).thumbnail(0.1f).centerCrop().placeholder(R.mipmap.global_default_image_outlets).error(R.mipmap.global_default_image_outlets).into(holder.homeDetailGvItemIv);
        holder.homeDetailGvItemTvName.setText("￥"+name);
        holder.homeDetailGvItemTvNPrice.setText("￥"+salePrice);
        holder.homeDetailGvItemTvOPrice.setText("￥"+marketPrice);
        holder.homeDetailGvItemTvDiscount.setText(discount);
        return convertView;
    }


    static class ViewHolder {
        @Bind(R.id.home_detail_gv_item_iv)
        ImageView homeDetailGvItemIv;
        @Bind(R.id.home_detail_gv_item_tvName)
        TextView homeDetailGvItemTvName;
        @Bind(R.id.home_detail_gv_item_tvNPrice)
        TextView homeDetailGvItemTvNPrice;
        @Bind(R.id.home_detail_gv_item_tvOPrice)
        TextView homeDetailGvItemTvOPrice;
        @Bind(R.id.home_detail_gv_item_tvDiscount)
        TextView homeDetailGvItemTvDiscount;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
